<template>
  <div class="container">
    <!-- 导航条 -->
    <van-nav-bar
      left-arrow
      @click-left="$router.back()"
      title="编辑资料">
    </van-nav-bar>

    <!-- 编辑区 -->
    <van-cell-group>
      <input type="file" hidden @change="hPhotoChange" ref="refPhoto" >
      <van-cell is-link title="头像"  @click="hShowSelectImage" center>
        <van-image
          slot="default"
          width="1.5rem"
          height="1.5rem"
          fit="cover"
          round
          :src="user.photo"
        />
      </van-cell>
        <!-- value： 设置右侧显示的文字 -->
      <van-cell is-link title="姓名" :value="user.name" @click="isShowName=true"/>

      <van-cell is-link title="性别" :value="user.gender === 1?'男':'女'" @click="showGender=true"/>

      <van-cell is-link title="生日" :value="user.birthday" @click="showBirthday=true"/>
    </van-cell-group>

    <!-- 修改名字的弹层 -->
    <van-dialog
      v-model="isShowName"
      title="修改名字"
      show-cancel-button
      @confirm="hSaveName">
      <van-field v-model.trim="newName" />
    </van-dialog>

    <!-- 修改性别 -->
      <van-popup v-model="showGender" position="bottom">
        <van-nav-bar title="修改性别" left-text="取消" @click-left="showGender=false">
        </van-nav-bar>
        <van-cell title="男" @click="hSaveGender(0)" is-link></van-cell>
        <van-cell title="女" @click="hSaveGender(1)" is-link></van-cell>
      </van-popup>

      <!-- 修改生日 -->
      <van-popup v-model="showBirthday" position="bottom">
        <van-nav-bar title="修改生日">
        </van-nav-bar>
        <van-datetime-picker
          v-model="newDate"
          type="date"
          title="选择年月日"
          @cancel="showBirthday=false"
          @confirm="hSaveBirthday"
          :min-date="minDate"
          :max-date="maxDate"
        />
      </van-popup>
  </div>
</template>

<script>
import dayjs from 'dayjs'
import { getUserInfo, updateUserInfo, updatePhoto } from '@/api/user.js'
export default {
  name: 'userProfile',
  data () {
    return {
      // 控制弹层
      isShowName: false,
      showGender: false,
      showBirthday: false,

      // 当前用户的信息
      user: {},

      // 修改后的新名字
      newName: '',
      // 修改后新生日
      newDate: new Date(),
      minDate: new Date(1965, 0, 10), // dateTime-picker中最小时间
      maxDate: new Date() // 当前时间
    }
  },
  created () {
    this.loadUserInfo()
  },
  methods: {
    // 用户在头像上点击，希望也弹出文件选择框
    // 移花接木
    hShowSelectImage () {
      // 就相当于直接在input type="file" 上点击
      this.$refs.refPhoto.click()
    },
    // 修改用户头像
    async hPhotoChange () {
      // 获取当前的头像文件
      // console.dir(this.$refs.refPhoto)
      const file = this.$refs.refPhoto.files[0]
      if (!file) {
        return
      }
      try {
        // 由于上传是文件，需要用FormData动态构建参数
        const fd = new FormData()
        fd.append('photo', file)
        const res = await updatePhoto(fd)
        this.user.photo = res.data.data.photo

        // 保存到vuex
        this.$store.commit('mSetUserPhoto', this.user.photo)
        this.$toast.success('修改头像成功')
      } catch (err) {
        this.$toast.fail('修改头像失败')
      }
    },
    // 修改用户生日
    async hSaveBirthday () {
      const birthday = dayjs(this.newDate).format('YYYY-MM-DD')
      console.log(birthday)

      try {
        const res = await updateUserInfo({
          birthday
        })
        console.log(res)
        this.user.birthday = birthday
        this.showBirthday = false
        this.$toast.success('修改用户生日成功')
      } catch (err) {
        this.$toast.fail('修改用户生日失败')
      }
    },
    // 修改用户性别
    async hSaveGender (gender) {
      try {
        const res = await updateUserInfo({
          gender
        })
        console.log(res)
        this.user.gender = gender
        this.$toast.success('修改性别成功')
        this.showGender = false
      } catch (err) {
        this.$toast.fail('修改性别失败')
      }
    },
    // 修改用户姓名
    async hSaveName  () {
      if (this.newName === '') {
        return
      }
      try {
        const res = await updateUserInfo({
          name: this.newName
        })
        console.log(res)
        this.user.name = this.newName
        this.newName = ''
        this.$toast.success('修改名字成功')
      } catch (err) {
        this.$toast.fail('修改名字失败')
      }
    },
    // 获取用户的个人信息
    async loadUserInfo () {
      const res = await getUserInfo()
      console.log(res)
      this.user = res.data.data
    }
  }
}
</script>
